package com.sjm.bookshop.controller.client;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjm.bookshop.entity.Address;
import com.sjm.bookshop.entity.Book;
import com.sjm.bookshop.entity.BookS;
import com.sjm.bookshop.entity.Slide;
import com.sjm.bookshop.service.client.ClientService;
import com.sjm.bookshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/client")
public class ClientCtrl {
    @Autowired
    private ClientService clientService;


    //轮播列表
    @RequestMapping("/showSlide")
    public Result show() {
        List<Slide> slides = clientService.get(1);
        return Result.success(200, slides, "轮播图");
    }

    //    商品展示
    @RequestMapping("/bookShow")
    public Result bookShow() {
//      在售卖的图书
        PageHelper.startPage(1, 15);
        List<BookS> bookList = clientService.getBooks(1);
        PageInfo<BookS> pageInfo_bookList = new PageInfo<>(bookList);
//     在售卖且被推荐的图书
        PageHelper.startPage(1, 10);
        List<BookS> bookByRecommend = clientService.getbookByRecommend(1, 1);
        PageInfo<BookS> pageInfo_bookByRecommend = new PageInfo<>(bookByRecommend);
//     封装结果
        Map<String, Object> map = new HashMap<>();
        map.put("bookList", pageInfo_bookList);
        map.put("bookByRecommend", pageInfo_bookByRecommend);
        return Result.success(200, map, "刷新成功");
    }

    //    商品展示
    @RequestMapping("/showMore/{param}")
    public Result showMore(@PathVariable("param") String param) {
        if (param.equals("recommend")) {
//            查询更多推荐
//            PageHelper.startPage(1, 10);
            List<BookS> Recommend = clientService.getMoreRecommend();
//            PageInfo<BookS> pageInfo_1 = new PageInfo<>(Recommend);
            return Result.success(200, Recommend, "更多推荐 ");
        } else if (param.equals("list")) {
//            查询更多图书
            List<BookS> moreBookList = clientService.getMoreBookList();
            return Result.success(200, moreBookList, "更多图书 ");
        } else {
            return Result.success(201, "错误请求 ");
        }
    }

//    查询图书
    @RequestMapping("/search/{keyword}")
    public Result search(@PathVariable String keyword){
        if (keyword!=null){
            List<Book> bookList = clientService.searchBook(keyword);
            return Result.success(200,bookList,"为您查询到以下结果");
        }else {
            return Result.fail(201,"未查询到结果");
        }

    }
    //    图书详情
    @RequestMapping("/bookDetail/{bookId}")
    public Result showMore(@PathVariable("bookId") int bookId) throws ParseException {
        if (bookId > 0) {
            BookS bookDetail = clientService.getDetail(bookId, 1);
//            相关推荐
            PageHelper.startPage(1, 15);
            List<BookS> relative = clientService.relative(bookDetail.getType(), 1);
            PageInfo<BookS> pageInfo_relative = new PageInfo<>(relative);
            Map<String, Object> map = new HashMap<>();
            map.put("bookDetail", bookDetail);
            map.put("pageInfo_relative", pageInfo_relative);
            return Result.success(200, map, "success");
        } else {
            return Result.fail(201, "当前书本已下架");
        }

    }


    //  获取用户地址
    @RequestMapping("/address/{id}")
    public Result getAddress(@PathVariable("id") int id) {
        List<Address> address = clientService.getAddress(id);
        if (address.size() > 0) {
            return Result.success(200, address, "刷新成功");
        } else {
            return Result.fail(201, "刷新失败");
        }

    }

    //    根据地址ID删除
    @RequestMapping("/address/del/{id}")
    public Result delAddress(@PathVariable("id") int id) {
        int del = 0;
        if (id > 0) {
            del = clientService.del(id);
        }
        if (del > 0) {
            return Result.success(200, "删除成功");
        } else {
            return Result.fail(201, "删除失败");
        }

    }

    //    修改
    @RequestMapping("/address/update")
    public Result delAddress(@RequestBody Address address) {
        if (address != null) {
            int res = clientService.updateAddress(address);
            if (res > 0) {
                return Result.success(200, "修改成功");
            } else {
                return Result.fail(201, "修改失败");
            }
        } else {
            return Result.fail(202, "网络错误");
        }

    }

    //    修改
    @RequestMapping("/address/add")
    public Result addAddress(@RequestBody Address address) {
        if (address != null) {
            int res = clientService.addAddress(address);
            if (res > 0) {
                return Result.success(200, "添加成功");
            } else {
                return Result.fail(201, "添加失败");
            }
        } else {
            return Result.fail(202, "网络错误");
        }

    }

}
